﻿using MediatR;
using SlaughterItem.Common.Domains.RKWeigh;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.RKWeighData_
{
    public class DelRKWeightHandler : IRequestHandler<DelRKWeightDataCommand, Result>
    {
        public DelRKWeightHandler(IBaseRepository<RKWeighData> rKWeighDataRepository)
        {
            RKWeighDataRepository = rKWeighDataRepository;
        }

        public IBaseRepository<RKWeighData> RKWeighDataRepository { get; set; }
        public async Task<Result> Handle(DelRKWeightDataCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CommonErrorCode.Delete};
            var list = RKWeighDataRepository.GetAll().FirstOrDefault(x=>x.RKWeighId==request.RKWeighId);
            if (list != null)
            {
                list.IsWeigh = true;
                RKWeighDataRepository.Update(list);
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
